// pages/member/withdrawEditmobile/withdrawEditmobile.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    form: {
      mobile: '',
      //验证码
      code: '',
      captcha: ""
    },
    action_name: '绑定',
    codetext: '获取短信验证码',
    start1: false,
    imgcode: ""
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {

  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  //绑定图形验证码的值
  captchainp(e) {
    let val = e.detail.value;
    this.setData({
      "form.captcha": val.trim()
    });
  },
  //绑定手机号的值
  mobileinp(e) {
    let val = e.detail.value;
    this.setData({
      "form.mobile": val.trim()
    });
  },
  //绑定验证码的值
  codeinp(e) {
    let val = e.detail.value;
    this.setData({
      "form.code": val.trim()
    });
  },
  sendCode(e) {
    if (this.data.start1) {
      return false;
    }
    let time = 60;
    let that = this;
    that.setData({
      "start1": true,
      "codetext": "(" + time + ")秒后重新获取"
    });
    let set = setInterval(function() {
      that.setData({
        codetext: "(" + --time + ")秒后重新获取",
      });
    }, 1000);
    setTimeout(function() {
      that.setData({
        codetext: "获取短信验证码",
        start1: false
      });
      clearInterval(set);
    }, 60000);
    that.verificationCode(set);
  },
  // 获取验证码
  verificationCode(set) {
    let that = this;
    if (app._isTextEmpty(this.data.form.captcha) && this.data.imgcode) {
      wx.showToast({
        icon: 'none',
        title: '请填图形验证码',
        duration: 1500
      });
      that.setData({
        codetext: "获取短信验证码",
        start1: false
      });
      clearInterval(set);
    } else if (app._isTextEmpty(this.data.form.mobile)) {
      wx.showToast({
        icon: 'none',
        title: '请填写手机号',
        duration: 1500
      });
      that.setData({
        codetext: "获取短信验证码",
        start1: false
      });
      clearInterval(set);
    } else {
      let urlStr = app.getNetAddresss("member.register.sendWithdrawCode");
      urlStr += '&mobile=' + this.data.form.mobile;
      urlStr += '&captcha=' + this.data.form.captcha;
      urlStr += '&code=' + this.data.form.code;
      //发送获取验证码的请求//旧的请求：member.register.sendCodeV2
      app._getNetWork({
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              icon: 'none',
              title: '已发送',
              duration: 1500
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
            //刷新图形验证码
            that.setData({
              'form.captcha': '',
            });
            that.getimgdata();
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
  //获取图片验证码
  getimgdata() {
    let that = this;
    let urlStr = app.getNetAddresss("home-page.index");
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.captcha) {
            that.setData({
              imgcode: res.data.captcha.img
            });
          }
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //提交修改手机的请求
  alterAction() {
    var that = this;
    //发送获取验证码的请求
    if (app._isTextEmpty(this.data.form.mobile)) {
      wx.showToast({
        icon: 'none',
        title: '手机号码不能为空',
        duration: 1500
      });
      return;
    }
    if (app._isMoblie(this.data.form.mobile)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确的手机号',
        duration: 1500
      });
      return;
    }
    if (app._isTextEmpty(this.data.form.code)) {
      wx.showToast({
        icon: 'none',
        title: '请填写验证码',
        duration: 1500
      });
      return;
    }
    if (this.data.imgcode) {
      if (app._isTextEmpty(this.data.form.captcha)) {
        wx.showToast({
          icon: 'none',
          title: '图形验证码不能为空',
          duration: 1500
        });
        return;
      }
    }
    let urlStr = app.getNetAddresss("member.member.bindWithdrawMobile");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: that.data.form,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500,
            success: function() {
              setTimeout(function() {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {}
    });
  },
});
